//
//  MZAppDelegate.m
//  minzuwenhuagong
//
//  Created by WANG CUNRUI on 14-5-22.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZAppDelegate.h"
#import <UIKit/UIKit.h>
#import "Reachability.h"


@implementation MZAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
@synthesize navigationControllerMessage = _navigationControllerMessage;
@synthesize navigationControllerMuseum = _navigationControlerMuseum;
@synthesize navigationControllerDimensionCode = _navigationControlerDimensionCode;
@synthesize navigationControllerSever = _navigationControlerSever;




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    
    
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    // 判断通知是否存在
    if (notification) {
        NSLog(@"11%@", notification.userInfo);
        [notification.userInfo writeToFile:@"/users/kobehjk/Desktop/123.plist" atomically:YES];
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds] ];
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.1506 green:0.6073 blue:0.5494 alpha:1.0]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    //定义tabBar
    //_tabBarController = [[UITabBarController alloc]init];
    _tabBarController = [[UITabBarController alloc] init];
    CGRect frame = self.window.bounds;
    _tabBarController.tabBar.backgroundColor = [UIColor colorWithRed:0.9961 green:0.9373 blue:0.9216 alpha:1];
    _tabBarController.tabBar.frame = CGRectMake(0, frame.size.height - 50, 320, 50);
    CGRect frame1 = CGRectMake(0,0,320,48);
    UIView * v = [[UIView alloc]initWithFrame:frame1];
    [v setBackgroundColor:[[UIColor alloc]initWithRed:0.9961 green:0.9373 blue:0.9216 alpha:1]];
    [_tabBarController.tabBar insertSubview:v atIndex:0];
    //定义首页
    MZMessageViewController *messageViewController = [[MZMessageViewController alloc] init];
    messageViewController.title = @"民族文化宫展讯";
    UITabBarItem *messageItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"53-house.png"] tag:1];
    messageViewController.tabBarItem = messageItem;
    _navigationControllerMessage = [[UINavigationController alloc] initWithRootViewController:messageViewController];
    
    //定义博物馆
    MZMuseumViewController *museumViewController = [[MZMuseumViewController alloc] init];
    museumViewController.title = @"民族文化宫博物馆";
    UITabBarItem *museumItem = [[UITabBarItem alloc] initWithTitle:@"博物馆" image:[UIImage imageNamed:@"42-photos.png"] tag:2];
    museumViewController.tabBarItem = museumItem;
    _navigationControlerMuseum = [[UINavigationController alloc] initWithRootViewController:museumViewController];
    
    //定义二维码
    MZDimensionCodeViewController *dimensioncodeViewController = [[MZDimensionCodeViewController alloc] init];
    dimensioncodeViewController.title = @"二维码扫描";
    UITabBarItem *dimensioncodeItem = [[UITabBarItem alloc] initWithTitle:@"二维码" image:[UIImage imageNamed:@"86-camera.png"] tag:3];
    dimensioncodeViewController.tabBarItem = dimensioncodeItem;
    _navigationControlerDimensionCode = [[UINavigationController alloc] initWithRootViewController:dimensioncodeViewController];
    
    //定义展厅
    MZExhibitionRoomViewController *exhibitionroomViewController = [[MZExhibitionRoomViewController alloc] init];
    exhibitionroomViewController.title = @"博物馆展厅";
    UITabBarItem *exhibitionroomItem = [[UITabBarItem alloc] initWithTitle:@"展厅" image:[UIImage imageNamed:@"07-map-marker.png"] tag:4];
    exhibitionroomViewController.tabBarItem = exhibitionroomItem;
    _navigationControllerExhibitionRoom = [[UINavigationController alloc] initWithRootViewController:exhibitionroomViewController];
    
   //定义服务中心
   MZServeViewController *serveViewController = [[MZServeViewController alloc] init];
   serveViewController.title = @"服务中心";
   UITabBarItem *serveItem = [[UITabBarItem alloc] initWithTitle:@"服务中心" image:[UIImage imageNamed:@"112-group.png"] tag:5];
   serveViewController.tabBarItem = serveItem;
   _navigationControlerServe = [[UINavigationController alloc] initWithRootViewController:serveViewController];
    
    //填充tabBar
  NSArray *tabControllers = [NSArray arrayWithObjects:_navigationControllerMessage,_navigationControlerMuseum,_navigationControlerDimensionCode,_navigationControllerExhibitionRoom,_navigationControlerServe,nil];
  _tabBarController.viewControllers = tabControllers;
    
    [_tabBarController setSelectedIndex:0];
    UIColor *color = [UIColor whiteColor];
    _tabBarController.tabBar.translucent = NO;
    _tabBarController.tabBar.barTintColor = color;
    _tabBarController.tabBar.tintColor = [UIColor colorWithRed:0.1686 green:0.6073 blue:0.5494 alpha:1.0];
                                          //colorWithRed:0/255.0 green:75/255.0 blue:48/255.0 alpha:1.0];

    self.window.rootViewController = _tabBarController;
    [self.window addSubview:_tabBarController.view];
    
    _myCache = [[ASIDownloadCache alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    [_myCache setStoragePath:[documentDirectory stringByAppendingString:@"resource"]];
    [_myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
//    self.isReachable = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
    [hostReach startNotifier];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"1212%@", deviceToken);
    // 1. 从系统偏好取之前的token
    NSData *oldToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"];
    // 2. 新旧token进行比较
    if (![oldToken isEqualToData:deviceToken]) {
        // 3. 如果不一致，保存token到系统偏好
        [[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:@"deviceToken"];
        
        // 4. 使用post请求传输新旧token至服务器
        // 1) url
        // 具体的URL地址以及POST请求中的参数和格式，是由公司的后端程序员提供的
        // 2) request POST body（包含新旧token的数据）
        // 3) connection 的异步
    }
}
- (void)reachabilityChanged:(NSNotification *)note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    NetworkStatus status = [curReach currentReachabilityStatus];
    self.isReachable = YES;
    if (status == NotReachable) {

        self.isReachable = NO;
    }
    else
    {
        self.isReachable = YES;
    }
    
    
}

- (void)loadNotification
{
//    UILocalNotification *notification = [[UILocalNotification alloc]init];
//    // 5秒钟之后触发
//    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
//    notification.alertBody = @"通知测试";
//    notification.soundName = UILocalNotificationDefaultSoundName;
//    notification.applicationIconBadgeNumber = 1;
//    
//    notification.userInfo = @{@"userName": @"kobe", @"age": @(18)};
    
    // 要调用本地通知，需要通过UIApplication来统一调度
//    [[UIApplication sharedApplication]scheduleLocalNotification:notification];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [application setApplicationIconBadgeNumber:0];
}

#pragma mark - 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self loadNotification];
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}




@end
